package com.qf.zyh.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author: COS
 * @time: 2021/10/26 11:27
 * @description: 提供数据库链接, 事务的工具类
 */
public class DBUtil {
    //数据库连接池
    private static DruidDataSource ds;
    //ThreadLocal对象
    private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();

    static {
        try {
            //创建properties对象
            Properties pro = new Properties();
            pro.load(DBUtil.class.getResourceAsStream("/jdbc.properties"));
            //创建ds数据源
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/10/26 11:31
     * @description: 获取链接的方法
     */
    public static Connection getConnection() {
        // 1.在线程中获取链接对象
        Connection con = THREAD_LOCAL.get();
        try {
            //2 判断是否为null
            if (con == null) {
                //3如果是null就在链接池中获取链接
                con = ds.getConnection();
                //4 设置链接到线程中
                THREAD_LOCAL.set(con);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //5 返回con对象
        return con;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/10/26 11:34
     * @description: 关闭链接所有的
     */
    public static void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
                //清除线程中的数据
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/10/26 11:35
     * @description: 单独关闭链接=>为了事务使用
     */
    public static void closeCon() {
        Connection con = getConnection();
        try {
            con.close();
            //清除线程中的数据
            THREAD_LOCAL.remove();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/10/26 11:36
     * @description: 开启事务
     */
    public static void begin() {
        Connection con = getConnection();
        try {
            con.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/10/26 11:36
     * @description: 提交事务
     */
    public static void commit() {
        Connection con = getConnection();
        try {
            con.commit();
            // 演示都可以使用所以和rollback不一样
            closeAll(con, null, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/10/26 11:36
     * @description: 回滚
     */
    public static void rollback() {
        Connection con = getConnection();
        try {
            con.rollback();
            closeCon();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
